package com.example.bbs.service.impl;

import com.example.bbs.entity.Disease;
import com.example.bbs.entity.DiseaseTag;
import com.example.bbs.entity.Doctor;
import com.example.bbs.entity.User;
import com.example.bbs.mapper.DiseaseMapper;
import com.example.bbs.mapper.DiseaseTagMapper;
import com.example.bbs.mapper.DoctorMapper;
import com.example.bbs.mapper.UserMapper;
import com.example.bbs.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jdk.nashorn.internal.codegen.ObjectClassGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import java.util.HashMap;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jiaziyun
 * @since 2021-04-30
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DoctorMapper doctorMapper;

    @Autowired
    private DiseaseTagMapper diseaseTagMapper;

    @Override
    public boolean getUserInfo(String id, Model model) {
        try {
            User user = userMapper.selectById(Integer.parseInt(id));
            if (user.getType() == 2) {
                Doctor doctor = doctorMapper.selectById(user.getId());
                model.addAttribute("doctor", doctor);
                model.addAttribute("selectdiseasetagid", doctor.getDiseaseId());
            }
            model.addAttribute("userinfo", user);
            return true;
        } catch (Exception e) {
            return false;
        }


    }

    @Override
    public String updateBaseInfo(HashMap<String, String> parmap) {
        try {
            User user = new User();
            user.setId(Integer.parseInt(parmap.get("id")));
            user.setName(parmap.get("name"));
            user.setDescription(parmap.get("description"));
            user.setAge(Integer.valueOf(parmap.get("age")));
            user.setSex(Integer.valueOf(parmap.get("sex")));
            user.setAddress(parmap.get("address"));
            user.setEmail(parmap.get("email"));
            int i = userMapper.updateById(user);
            if (i > 0) {
                return "200";
            }
            return "500";
        } catch (Exception e) {
            return "400";
        }
    }

    @Override
    public int updateUserAvator(int id, String path) {
        System.out.println(id);
        System.out.println(path);
        User user = new User();
        user.setId(id);
        user.setImg(path);
        userMapper.updateById(user);

        return 0;
    }
}
